package FirstQuestion;

/**
 * 定义一个长度为[16][16]的整型二维数组并输入所有位置的元素值，分别实现二维数组中所有行和所有列中所有元素的累加和并打印。
 * 再分别实现二维数组中左上角到右下角和右上角到左下角所有元素的累加和并打印。
 * @author cyt
 * @date 2020/6/8
 */
public class FirstQuesiton{

    public int[][] arr = new int[16][16];

    public int[][] getArr() {
        return arr;
    }

    //初始化数组，每个元素的值时1到9的random值
    public static void arrInit(int[][] arr){
        for(int i = 0; i <= arr.length - 1; i++){

            for (int j = 0; j <= arr[i].length - 1; j++){
                arr[i][j] = (int)(Math.random() * 9 + 1);
            }
        }
    }

    public void show(){
        for (int i = 0; i < this.arr.length; i++) {
            for (int j = 0; j < this.arr[i].length; j++) {
                System.out.print(this.arr[i][j]+" ");
            }
            System.out.println();

        }
    }
    //打印所有行的元素累加和
    public void printAllRows(){
        for(int i = 0; i <= this.arr.length - 1; i++){
            int sum = 0;
            for ( int j = 0; j <= this.arr[i].length - 1; j++){
                sum += this.arr[i][j];
            }
            System.out.println("第" + i + "行的所有元素的和为："+sum);
        }
    }
    //打印所有列的元素累加和
    public void printAllColumn(){
        for (int i = 0; i <= this.arr[0].length - 1; i++) {
            int sum = 0;
            for (int j = 0; j <= this.arr.length - 1 ; j++) {
                sum += this.arr[j][i];
            }
            System.out.println("第" + i + "列的所有元素的和为："+sum);
        }
    }
    //打印从左上到右下的元素累加和
    public void printFromLeftUpToRightButtom(){
        int sum = 0;
        for (int i = 0; i <= this.arr.length - 1; i++) {
            sum += this.arr[i][i];
        }
        System.out.println("从左上角到右下角的所有元素的和为："+sum);
    }
    //打印从忧伤到左下的元素累加和
    public void printFromRightUpToLeftButtom(){
        int sum = 0;
        for (int i = 0; i <= this.arr.length - 1; i++) {
            sum += this.arr[i][this.arr.length - 1 - i];
        }
        System.out.println("从右上角到左下角的所有元素的和为："+sum);
    }


}
